package db;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.Logs;
import com.Person;
import com.SqliteExec;

public class fuxi {

	private static SqliteExec mSqliteExec= SqliteExec.getInstance(); 
	private String sqlBeforeExec=null;
	private String sqlExec = null;
	static String tableName="";
	public int count=0;
	public String[] nameList;
	public Object[] valueList;
	public boolean  isFirstSave=true;
	
	
	public  fuxi(){
		
		keyInit();
		createTable();
	}
	
	public void createTable(){
		
		
		if( !mSqliteExec.isTableExist(tableName)){
			
			String createSql = "create table if not exists  "+tableName+" ( id integer primary key autoincrement,";
			int i=0;
			for( i=0; i< count-1;i++){
				
				createSql +=nameList[i]+" string ,"; 
			}
			createSql +=nameList[i]+" string );"; 
			Logs.i("createTable not have it will be create :"+createSql);
			mSqliteExec.createTable(createSql);
		}else{
			Logs.i("table already exists");
		}
	}
	public void  keyInit(){
		
		
		
		Class c = getClass();
		String name = c.getName();
		this.tableName = name.substring(name.lastIndexOf(".")+1,name.length()).toLowerCase();

		Field[] fs =  c.getDeclaredFields();
		if(count == 0){
		count = fs.length;
		}

	
			if(nameList == null){
				
				nameList = new String[count];
				
			}
			Logs.i("this is first to save");
			for(int i=0; i< count; i++){
				
				Field f = fs[i];
				f.setAccessible(true);
				nameList[i] = f.getName();
			}
		
		this.sqlBeforeExec="insert into " + tableName + "(";
		for(int i=0 ; i < count  ;i++){
			
			if( i == count -1){
				
				this.sqlBeforeExec+=nameList[i]+") values('";
				break;
				
			}
			this.sqlBeforeExec+=nameList[i]+",";
		}
		
		Logs.i("sql "+this.sqlBeforeExec);
	
	}
	
	
	public void getValue(){
		
		if(mSqliteExec == null){
		
			this.mSqliteExec = SqliteExec.getInstance();
		
		}
		Class c = getClass();
		Field[] fs =  c.getDeclaredFields();
		
		int n=fs.length;
		
		valueList = new String[count];
		for(int i=0; i< n; i++){
			
			
			Field f = fs[i];
			f.setAccessible(true);
			try{
				Logs.i("name: "+f.getName()+"  value: "+f.get(this));
				
				valueList[i] = f.get(this);
				
				//Logs.i("name: "+nameList[i]+" value: "+valueList[i]);
			 }catch(Exception e){
				 
				 e.printStackTrace();
			 }
			
		}
		

		
	}//end getvalue();
	
	public  boolean save(){
		
		getValue();
		this.sqlExec = this.sqlBeforeExec;
		for(int i=0; i< count; i++){
			
			if( i == count-1){
				
				this.sqlExec+= this.valueList[i]+"')";
				break;
			}
			this.sqlExec+= this.valueList[i]+"','";
		}
		
		Logs.i("exec  sql : "+this.sqlExec);
		if(!mSqliteExec.insert(this.sqlExec)){
			
			Logs.i("insert failed");
			return false;
		}else{
			
			Logs.i("insert success");
			return true;
		}
		
	}
	
	public static <T> List<T> findAll(Class<T> modelClass ) {
	
		try{
		
			//already be a  class tyep so not use getClass
			String name = modelClass.getName();
			
			
			String tName= name.substring(name.lastIndexOf(".")+1,name.length()).toLowerCase();
			return SqliteExec.findAll(modelClass, tName) ;
			 
		}catch(NullPointerException e){
			
			throw e;
		}
		
		
	}
	

}
